use std::fmt::Display;

pub fn binding() {
    println!("Tell me what type of person you are");

    let p = match age() {
        0             => {println!("I haven't celebrated my first birthday yet"); Person::Baby},
        // 可以直接匹配（`match`） 1 ..= 12，但那样的话孩子会是几岁？
        // 相反，在 1 ..= 12 分支中绑定匹配值到 `n` 。现在年龄就可以读取了。
        n @ 1  ..= 12 =>{ println!("I'm a child of age {:?}", n); Person::Child},
        n @ 13 ..= 19 => {println!("I'm a teen of age {:?}", n); Person::Teen},
        // 不符合上面的范围。返回结果。
        n             => {println!("I'm an old person of age {:?}", n); Person::OldPerson},
    };
    println!("person:{}!!!", p);

    match some_number() {
        Some(n @ 42) => println!("The answer :{}!", n),
        Some(n) => println!("不感兴趣的值: {}.", n),
        _ => (),
    }
}

enum Person {
    Baby,
    Child,
    Teen,
    OldPerson,
}

impl Display for Person {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            Person::Baby => write!(f, "baby"),
            Person::Child => write!(f, "child"),
            Person::Teen => write!(f, "teen"),
            Person::OldPerson => write!(f, "old_person"),
        }
    }
}

// `age` 函数，返回一个 `u32` 值。
fn age() -> u32 {
    15
}

fn some_number() ->Option<u32> {
    Some(42)
}